package Pet.Management.common.utils;

import com.google.code.kaptcha.impl.DefaultKaptcha;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.Base64;

public class CaptchaUtils {

    public static String generateBase64Image(DefaultKaptcha defaultKaptcha, String code) {
        try {
            // 生成图片
            BufferedImage image = defaultKaptcha.createImage(code);
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            ImageIO.write(image, "jpg", outputStream);
            
            // 对字节数组进行Base64编码
            return Base64.getEncoder().encodeToString(outputStream.toByteArray());
        } catch (Exception e) {
            throw new RuntimeException("生成验证码图片失败", e);
        }
    }
} 